<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PDO
 *
 * @author Luki
 */
class Ftl_PDO extends PDO
{

    private static $_instance = null;
    private $_stmt = null;


    public function __construct($pAttrs=array())
    {

        $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_BASE;

        parent::__construct( $dsn, DB_USER, DB_PASS, $pAttrs );


    }

    public static function getInstance()
    {
        if( self::$_instance == null )
        {
            self::$_instance = new self(
                    array(
                            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
                            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                         )
            );
        }

        return self::$_instance;
    }

    public function insert($table,$data,$returnId=true)
    {

        $sql = 'INSERT INTO ' . $table ;

        foreach($data as $field => $value)
        {

        }




        echo $sql;
        return false;
        try
        {

            if( !$this->query( $sql ) ) {
                    return false;
            }


            if( $returnId ) {
                    return mysql_insert_id();
            }

            return true;


        }catch (Exception $e) {}

    }

    public function execute($sql)
    {



    }

    public function query($sql)
    {

        if ( trim ( $sql ) == '')
        {
            return false;
        }
        $count = $this->exec($sql);
        //$this->_stmt = parent::query($sql);
        
        //$stmt = $this->prepare($sql);
        //$stmt->execute();
        //$arrRetorno = $this->_stmt->fetchAll(PDO::FETCH_ASSOC);
        //$this->_stmt->closeCursor();
        return $count;

    }

    private function parseFields( $data )
    {

            $fields = array();

            foreach( $data as $field => $value ) {

                    if( $value === 'NOW()' ) {
                            $fields[] = $field . ' = NOW()';
                    } else if( is_null( $value ) ) {
                            $fields[] = $field . ' = NULL';
                    } else {

                            $value = trim( $value );
                            //$value = parent::scape( $value );

                            $fields[] = $field . ' = "' . $value . '"';

                    }

            }

            return ' SET ' . implode( ',', $fields );

    }

    

}
?>
